package com.action;

import com.hdb.DB;
import com.hdb.TGood;
import com.hdb.dao.Types;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;

import java.util.List;
import java.util.Map;

@ParentPackage("default")
@Namespace("/good")
@Results({
        @Result(name = "succeed", location = "/common/succeed.jsp"),
        @Result(name = "goodSearch", location = "/view/good/goodSearch.jsp"),
        @Result(name = "goodDetail", location = "/view/good/goodDetail.jsp"),
        @Result(name = "goodList", location = "/view/good/goodList.jsp"),
        @Result(name = "goodByClass", location = "/view/good/goodByClass.jsp"),
        @Result(name = "goodAll", location = "/view/good/goodByClass.jsp")
})
@InterceptorRef("defaultStack")/*设置默认过滤器*/
public class GoodAction extends BasicAction {
    private int goodId;
    private int classId;
    private String goodName;
    private DB db;
    /*查看物品详情*/
    @Action("goodDetail")
    public String goodDetail() {
        Map request = (Map) ServletActionContext.getContext().get("request");
        TGood good = db.goodDAO.findById(goodId);
        request.put("good", good);
        return "goodDetail";
    }
    /*按分类显示商品*/
    @Action("goodByClass")
    public String goodByClass() {
        int[] p = getP();
        List<TGood> goodList = db.goodByClass(classId, p[0], p[1]);
        Map req=getRequest();
        req.put("goodList", goodList);
        getPLink(goodList.size());
        return "goodByClass";
    }
    /*搜索商品*/
    @Action("goodSearch")
    public String goodSearch() {
        if (goodName.length() > 1) {
            List goodList = db.goodSearch(goodName, 0, 20);
            getRequest().put("goodList", goodList);
            getPLink(goodList.size());
        }
        return "goodSearch";
    }
    /*显示全部商品*/
    @Action("goodAll")
    public String goodAll() {
        int[] p = getP();
        Map request = getRequest();
        List goodList = db.goodDAO.find("goodState", Types.good.OnShelves, p[0], p[1]);
        request.put("goodList", goodList);
        getPLink(goodList.size());
        return "goodAll";
    }
    /*商品分类页面*/
    @Action("goodList")
    public String goodList() {
        Map request = getRequest();
        List classificationList = db.classDAO.findAll(0, 30);
        request.put("classList", classificationList);
        return "goodList";
    }

    public int getGoodId() {
        return goodId;
    }

    public void setGoodId(int goodId) {
        this.goodId = goodId;
    }

    public int getClassId() {
        return classId;
    }

    public void setClassId(int classId) {
        this.classId = classId;
    }

    public String getGoodName() {
        return goodName;
    }

    public void setGoodName(String goodName) {
        this.goodName = goodName;
    }

    public DB getDb() {
        return db;
    }

    public void setDb(DB db) {
        this.db = db;
    }
}
